package com.ling.zookeeper.zookeeperUtil;

import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestUtils {
	private final static Logger logger = LoggerFactory.getLogger(TestUtils.class);

	/**
	 * 打印列表
	 * 
	 * @param list
	 */
	public static void printList(List<String> list) {

		for (int index = 0; index < list.size(); index++) {
			logger.info("{}", list.get(index));
		}
	}

	/**
	 * 打印列表,突出显示包含项
	 * 
	 * @param list
	 */
	public static boolean printList(List<String> list, String item, String prefix) {
		boolean containsFlag = false;
		for (int index = 0; index < list.size(); index++) {
			if (list.get(index).equals(item)) {
				logger.info("{} contains item :[{}]", prefix, list.get(index));
				containsFlag = true;
			} else {
				logger.info("{}{}", prefix, list.get(index));
			}
		}
		return containsFlag;
	}

	/**
	 * 拼接字符串:将给定的路径合并成zk节点路径的形式
	 * 
	 * @param fPathStr
	 * @param sPathStr
	 * @return
	 */
	public static String strJoinToPath(String fPathStr, String sPathStr) {
		// 对第一个参数处理，是否以/开头 、是否以/结尾
		String resStr = "";
		if (!fPathStr.trim().startsWith("/")) {
			fPathStr = "/" + fPathStr.trim();
		}
		if (!fPathStr.endsWith("/")) {
			fPathStr = fPathStr + "/";
		}

		// 将拼接的路径拼接，并删除前后空格
		resStr = fPathStr + sPathStr.trim();

		// 拼接后的字符串是否以/结尾，若是则删除，因为zk节点路径最后不能用/结尾
		if (resStr.endsWith("/")) {
			resStr = resStr.substring(0, resStr.length() - 1);
		}

		// 清理不符合路径规则的字符，可能sPathStr传入时会带有/会导致拼接后出现//
		resStr = resStr.replace("//", "/");

		return resStr;
	}

	/**
	 * 获取perproty配置文件的配置项
	 * 
	 * @return
	 */
	public static Properties getDbProperties() {
		Properties prop = new Properties();
		try {
			// 读取属性文件a.properties
			InputStream in = TestUtils.class.getClassLoader().getResourceAsStream("db.properties");
			prop.load(in); /// 加载属性列表
			Iterator<String> it = prop.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				System.out.println(key + ":" + prop.getProperty(key));
			}
			in.close();

			/// 保存属性到b.properties文件
			/*
			 * FileOutputStream oFile = new FileOutputStream("b.properties",
			 * true);//true表示追加打开 prop.setProperty("phone", "10086");
			 * prop.store(oFile, "The New properties file"); oFile.close();
			 */
		} catch (Exception e) {
			System.out.println(e);
		}
		return prop;
	}

	/**
	 * 打印orderList列表
	 * 
	 * @param orderItemsList
	 */
	public static void printOrderItemList(List<String> orderItemsList) {

		for (int i = 0; i < orderItemsList.size(); i++) {
			logger.info(orderItemsList.get(i).toString());
		}
	}

	/**
	 * print map
	 * 
	 * @param orderItemsList
	 */
	public static void printHashmap(HashMap<String, String> hashMap) {
		for (String s : hashMap.keySet()) {
			logger.info("【{}】 {}", s, hashMap.get(s));
			System.err.println("【{"+s+"}】 {"+hashMap.get(s)+"}");
		}
	}

	public static HashSet<Integer> changeListToSet(List<Integer> orderUserId) {

		/*
		 * for (int i = 0; i < orderUserId.size(); i++) {
		 * logger.info(orderUserId.get(i).toString()); }
		 */
		HashSet<Integer> hashSet = new HashSet<>();
		hashSet.addAll(orderUserId);

		// System.out.println(hashSet);

		return hashSet;
	}

	public static void modifyProperty(String nodePath, String srcStr, String desStr, BaseZookeeper baseZookeeper) {
		ZkUtils.zkSetNodeData(nodePath, ZkUtils.zkGetNodeData(nodePath, baseZookeeper).replaceAll(srcStr, desStr),
				baseZookeeper);
	}

}
